import { http } from "@/utils/http";

export type BuildingType = {
  id: number;
  name: string;
  cellId: number;
  createTime: string;
  updateTime: string;
  tenantId: number;
};

export type BuildingItem = {
  id: number;
  name: string;
  orientation: string | null;
  heightM: number;
  structure: string | null;
  unitCount: number;
  floorCount: number;
  createdTime: string;
  updatedTime: string;
  housechragetype: string | null;
  buildingtypeId: number;
  tenantId: number;
};

export type BuildingListResult = {
  code: number;
  data: BuildingItem[];
  message: string;
};

export type BuildingTypeListResult = {
  code: number;
  data: BuildingType[];
  message: string;
};

export type BuildingCreateParams = {
  buildingtypeId: number;
  name: string;
  orientation: string;
  heightM: number;
  structure: string;
  unitCount: number;
  floorCount: number;
};

export type BuildingUpdateParams = {
  id: number;
  name: string;
  orientation: string;
  heightM: number;
  structure: string;
  unitCount: number;
  floorCount: number;
};

export type HouseItem = {
  unit: string;
  floor: string;
  room: number;
  roomId: number | null;
  status: number;
};

export type HouseManagementData = {
  unitList: string[];
  floorList: string[];
  roomList: HouseItem[];
};

export type HouseListResult = {
  code: number;
  data: HouseItem[];
  message: string;
};

export type HouseManagementResult = {
  code: number;
  data: HouseManagementData;
  message: string;
};

// 单元相关类型
export type UnitItem = {
  id: number;
  name: string;
};

export type UnitListResult = {
  code: number;
  data: UnitItem[];
  message: string;
};

// 楼层相关类型
export type FloorItem = {
  id: number;
  name: string;
};

export type FloorListResult = {
  code: number;
  data: FloorItem[];
  message: string;
};

// 添加房屋参数
export type HouseAddParams = {
  unitId: number;
  floorId: number;
  houseNum: number;
  orientation: string;
  floorSpace: string;
  insideSpace: string;
  sharedSpace: string;
  room: number;
  hall: number;
  toilet: number;
};

/** 获取楼栋类型列表 */
export const getBuildingTypeList = (communityId: number) => {
  return http.request<BuildingTypeListResult>(
    "get",
    `spBuilding/typeList/${communityId}`
  );
};

/** 获取楼栋类型下的所有楼栋 */
export const getBuildingList = (buildingtypeId: number) => {
  return http.request<BuildingListResult>(
    "get",
    `spBuilding/building/${buildingtypeId}`
  );
};

/** 创建楼栋 */
export const createBuilding = (data: BuildingCreateParams) => {
  return http.request<{ code: number; message: string }>(
    "post",
    "spBuilding/create",
    { data }
  );
};

/** 更新楼栋 */
export const updateBuilding = (data: BuildingUpdateParams) => {
  return http.request<{ code: number; message: string }>(
    "put",
    "spBuilding/update",
    { data }
  );
};

/** 删除楼栋 */
export const deleteBuilding = (id: number) => {
  return http.request<{ code: number; message: string }>(
    "delete",
    `spBuilding/delete/${id}`
  );
};

/** 获取楼栋房屋管理数据 */
export const getHouseManagement = (buildingId: number) => {
  return http.request<HouseManagementResult>(
    "get",
    `spHouse/HouseManagement?buildingId=${buildingId}`
  );
};

/** 获取单元列表 */
export const getUnitList = (buildingId: number) => {
  return http.request<UnitListResult>(
    "get",
    `spUnit/list?buildingId=${buildingId}`
  );
};

/** 获取楼层列表 */
export const getFloorList = (buildingId: number) => {
  return http.request<FloorListResult>(
    "get",
    `spFloor/list?buildingId=${buildingId}`
  );
};

/** 添加房屋 */
export const addHouse = (data: HouseAddParams) => {
  return http.request<{ code: number; message: string }>(
    "post",
    "spHouse/add",
    { data }
  );
};

/** 删除房屋 */
export const deleteHouse = (ids: number[]) => {
  return http.request<{ code: number; message: string }>(
    "delete",
    "spHouse/delete",
    { data: { ids } }
  );
};

// 更新房屋参数
export type HouseUpdateParams = {
  id: number;
  houseNum: number;
  floorSpace: string;
  insideSpace: string;
  sharedSpace: string;
  room: number;
  hall: number;
  toilet: number;
};

/** 更新房屋 */
export const updateHouse = (data: HouseUpdateParams) => {
  return http.request<{ code: number; message: string }>(
    "put",
    "spHouse/update",
    { data }
  );
};

// 房屋详情类型
export type HouseDetail = {
  id: number;
  number: number;
  unitId: number;
  floorId: number;
  orientation: string | null;
  floorSpace: string;
  insideSpace: string | null;
  sharedSpace: string | null;
  room: number;
  hall: number;
  toilet: number;
  updateTime: string;
  createTime: string;
  tenantId: number;
  ownerId?: number | null; // 后端用于查询入住人员
  renterId?: number | null; // 后端用于查询入住人员
};

export type HouseDetailResult = {
  code: number;
  data: HouseDetail;
  message: string;
};

/** 获取房屋详情 */
export const getHouseDetail = (roomId: number) => {
  return http.request<HouseDetailResult>("get", `spHouse/getById?id=${roomId}`);
};

/** 删除所有房屋 */
export const deleteAllHouses = (buildingId: number) => {
  return http.request<{ code: number; message: string }>(
    "delete",
    `spHouse/deleteAll?buildingId=${buildingId}`
  );
};

// 生成所有房屋参数类型
export type GenerateAllHousesParams = {
  buildingId: number;
  houseNumberOFUnits: number;
  orientation: string;
  floorSpace: string;
  insideSpace: string;
  sharedSpace: string;
  room: number;
  hall: number;
  toilet: number;
};

/** 生成所有房屋 */
export const generateAllHouses = (data: GenerateAllHousesParams) => {
  return http.request<{ code: number; message: string }>(
    "post",
    "spHouse/addAllHouse",
    { data }
  );
};

// 入住人员查询
export type PersonnelQueryParams = {
  ownerId: number | null;
  renterId: number | null;
  id: number; // 房屋ID
};

export type PersonnelItem = {
  payerType: 1 | 2; // 1 业主 2 租户
  id: number;
  name: string;
  phone: string;
  email: string;
};

export type PersonnelQueryResult = {
  code: number;
  data: PersonnelItem | null; // 可能为空
  message: string;
};

/**
 * 根据房屋信息查询入住人员
 */
export const queryPersonnelByHouseId = (data: PersonnelQueryParams) => {
  return http.request<PersonnelQueryResult>(
    "post",
    "spHouse/queryPersonnelByHouseId",
    { data }
  );
};
